<?php
/**
 * Created by PhpStorm.
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
class Cangku extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['ck_name|ck_code|ck_remark|ck_phone|ck_address'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('cangku')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('cangku')
                ->where($where)
                ->order('ck_time desc')
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'ck_code' => $ck_code,
                'ck_remark' => $ck_remark,
                'ck_phone' => $ck_phone,
                'ck_name' => $ck_name,
                'ck_address' => $ck_address,
                'ck_type' => $ck_type,
                'ck_status' => 1,
                'ck_time' => date('Y-m-d H:i:s'),
                'ck_update' => date('Y-m-d H:i:s')
            ];
            if($ck_type > 0){
                Db::table('up_cangku')
                    ->where('ck_type',$ck_type)
                    ->setField('ck_type',0);
            }
            $res = Db::table('up_cangku')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function update(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'ck_code' => $ck_code,
                'ck_remark' => $ck_remark,
                'ck_phone' => $ck_phone,
                'ck_name' => $ck_name,
                'ck_address' => $ck_address,
                'ck_type' => $ck_type,
                'ck_update' => date('Y-m-d H:i:s')
            ];
            if($ck_type > 0){
                Db::table('up_cangku')
                    ->where('ck_type',$ck_type)
                    ->setField('ck_type',0);
            }
            $res = Db::table('up_cangku')->where('ck_id', $ck_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $ck_id = input('ck_id');
        // banner
        $banner = Db::table('up_cangku')
            ->where('ck_id', $ck_id)
            ->find();
        $this->assign([
            'ck_id' => $ck_id,
            'data' => $banner
        ]);
        return $this->fetch();
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $banner_id = input('id');
        $res = Db::table('up_cangku')->where('ck_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
}